package com.quickcancellation.crm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.quickcancellation.crm.config.LogAspect;
import com.quickcancellation.crm.constant.interfaceConstant.CodeConstant;
import com.quickcancellation.crm.pojo.dto.PageParamsDto;
import com.quickcancellation.crm.pojo.dto.ResponseDto;
import com.quickcancellation.crm.pojo.entity.Dictionary;
import com.quickcancellation.crm.service.IDictionaryService;
import com.quickcancellation.crm.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2023-02-18
 */
@RestController
@CrossOrigin
@RequestMapping("/dictionary")
@Api(tags = "字典值模块")
@SaCheckLogin
@SaCheckRole("dictionary")
public class DictionaryController {

    private final static Logger logger = LoggerFactory.getLogger(DictionaryController.class);
    @Resource
    private IDictionaryService dictionaryService;

    @LogAspect
    @ApiOperation(value = "查询字典值列表")
    @PostMapping("/getDic")
    public ResponseDto getDictionaryList(@RequestBody PageParamsDto<Dictionary> params) {

        //logger.info("入参是:: " + JSONObject.toJSONString(params));

        IPage<Dictionary> dictionaryIPage = dictionaryService.selectByDicList(params);

        return ResponseUtil.success(dictionaryIPage.getTotal(), dictionaryIPage.getRecords());

    }

    @LogAspect
    @ApiOperation(value = "更新字典值")
    @PostMapping("/updateDic")
    public ResponseDto updateDictionary(@RequestBody Dictionary params) {

        //logger.info("入参是:: " + JSONObject.toJSONString(params));

        // 获取字典值
        String dicVal = params.getDicVal();

        // 判空
        if (!StringUtils.hasLength(dicVal)) {
            return ResponseUtil.fail(CodeConstant.PARAM_NULL_CODE, "字典值为空");
        }

        // 判断字典值是否重复
        if (dictionaryService.existsDictionary(params.getId(), dicVal)) {

            return ResponseUtil.fail(CodeConstant.DATA_REPEAT_CODE, "字典值已存在");

        }
        boolean result = dictionaryService.updateById(params);

        return result ? ResponseUtil.success() : ResponseUtil.fail(CodeConstant.UPDATE_FAIL_CODE, "更新失败");

    }
    @LogAspect
    @ApiOperation(value = "添加字典值")
    @PostMapping("/saveDic")
    public ResponseDto saveDictionary(@RequestBody Dictionary params) {

        //logger.info("入参是:: " + JSONObject.toJSONString(params));

        if (dictionaryService.existsDictionary(null, params.getDicVal())) {
            return ResponseUtil.fail(CodeConstant.DATA_REPEAT_CODE, "字典值已存在");
        }

        params.setDicKey(dictionaryService.getNewDicKey());

        boolean result = dictionaryService.save(params);

        return result ? ResponseUtil.success() : ResponseUtil.fail(CodeConstant.SAVE_FAIL_CODE, "新增失败");

    }
    @LogAspect
    @ApiOperation(value = "删除字典值")
    @PostMapping("/deleteDic")
    public ResponseDto deleteDictionary(@RequestBody Dictionary params) {

        //logger.info("入参是:: " + JSONObject.toJSONString(params));

        boolean result = dictionaryService.removeById(params.getId());

        return result ? ResponseUtil.success() : ResponseUtil.fail(CodeConstant.DELETE_FAIL_CODE, "删除失败");


    }


}
